package org.acghub.mtdb.common.type;


import org.acghub.mtdb.common.Type;
import org.acghub.mtdb.util.StringUtil;

import java.util.Set;
import java.util.function.Function;

/**
 * @author zhouhe
 */
public abstract class TypeBasePrimitives extends Type {

    protected TypeBasePrimitives(String name) {
        super(name);
    }

    @Override
    public boolean isConstType() {
        return true;
    }

    @Override
    public void depends(Set<Type> types) {
        types.add(this);
    }

    @Override
    public boolean resolve() {
        return true;
    }

    @Override
    public void supportDefaultValue(String value) {
        try {
            Object o = this.checkDefaultValueFunc().apply(value);
            if (o == null) {
                throw new IllegalArgumentException(getName() + "类型默认值错误，default:" + value);
            }
        } catch (Exception e) {
            throw new IllegalArgumentException(getName() + "类型默认值错误，default:" + value, e);
        }
    }

    protected abstract Function<String, Object> checkDefaultValueFunc();

    @Override
    public String javaInterfaceType() {
        return StringUtil.upperFirstChar(this.getName());
    }

    @Override
    public String javaImplType() {
        return javaInterfaceType();
    }

    @Override
    public String getDefaultValue(String value) {
        return StringUtil.isNotEmpty(value) ? this.dealDefaultValue(value) : this.getDefaultValue();
    }

    protected String dealDefaultValue(String value) {
        return value;
    }

    protected abstract String getDefaultValue();
}
